package myservlet.control;
import mybean.data.*;
import java.sql.*;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.WebServlet;

@WebServlet("/super_login")
public class super_login extends HttpServlet{
    public void init(ServletConfig config) throws ServletException{
        super.init(config);
        try{
            Class.forName("com.mysql.jdbc.Driver");
        }
        catch(Exception e){}
    }
    public String handleString(String s){
        try{  byte bb[]=s.getBytes("iso-8859-1");
            s=new String(bb);
        }
        catch(Exception ee){}
        return s;
    }
    public void doPost(HttpServletRequest request,HttpServletResponse response)
            throws ServletException,IOException{
        Connection con;
        Statement sql;
        String logname=request.getParameter("logname").trim(),
                password=request.getParameter("password").trim();
        logname=handleString(logname);
        password=handleString(password);
        String uri="jdbc:mysql://127.0.0.1/web_used?"+
                "user=root&password=root&characterEncoding=gb2312";
        boolean boo=(logname.length()>0)&&(password.length()>0);


        super_manager_Bean superLoginBean = new super_manager_Bean();
        HttpSession session = request.getSession();
        session.setAttribute("superLoginBean", superLoginBean);

        try{
            con=DriverManager.getConnection(uri);
            String condition="select * from superAccount where username = '"+logname+
                    "' and password ='"+password+"'";
            sql=con.createStatement();
            if(boo){
                ResultSet rs=sql.executeQuery(condition);
                boolean m=rs.next();
                if(m==true){//调用登录成功的方法:
                    superLoginBean.setUsername(logname);
                    superLoginBean.setPassword(password);
                    RequestDispatcher dispatcher=
                            request.getRequestDispatcher("/system_operate/super_index.jsp");//转发
                    dispatcher.forward(request,response);
                }
                else{
                    String backNews="您输入的管理员用户名不存在，或密码不般配";
                    //调用登录失败的方法:
                    fail(request,response,logname,backNews);
                }
            }
            else{
                String backNews="请输入管理员用户名和密码";
                fail(request,response,logname,backNews);
            }
            con.close();
        }
        catch(SQLException exp){
            String backNews=""+exp;
            fail(request,response,logname,backNews);
        }
    }
    public  void  doGet(HttpServletRequest request,HttpServletResponse response)
            throws ServletException,IOException{
        doPost(request,response);
    }


    public void fail(HttpServletRequest request,HttpServletResponse response
            ,String logname,String backNews) {
        response.setContentType("text/html;charset=GB2312");
        try {
            PrintWriter out=response.getWriter();
            out.println("<html>");
            out.println("<head><style>body{text-align: center;background: #90d7ec;margin:300px}</style></head>");
            out.println("<body>");
            out.println("<h2>"+logname+"登录反馈结果<br>"+backNews+"</h2>") ;
            out.println("返回登录页面<br>");
            out.println("<a href =/Vice_School_ShopWeb_war_exploded/system_operate/super_login.jsp>管理员登录页面</a>");
            out.println("</body></html>");
        }
        catch(IOException exp){}
    }
}
